package com.sl.biz.modular.process.helper;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.core.util.ZipUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Map;


@Slf4j
public class ProcessCompObjectInitFileHelper {


    public static final ProcessCompObjectInitFileHelper INSTANCE = new ProcessCompObjectInitFileHelper();


    public String initCompFile(String code, String desc, String version) {

        //下载demo删除 target然后 更改包名，然后压缩，然后存储
        byte[] buffer = IoUtil.readBytes(URLUtil.getStream(ClassUtil.getResourceURL("square-lake-plugin-step-demo.zip")));

        File baseRootFile = getBaseRootFile();

        File compRootFile = FileUtil.file(baseRootFile, code + "_" + version + ".zip");

        FileUtil.writeBytes(buffer, compRootFile);

        File unzip = ZipUtil.unzip(compRootFile);

        //删除target
        FileUtil.del(FileUtil.file(unzip, "target"));

        File source = FileUtil.file(unzip, "source");

        File[] files = source.listFiles();
        for (File file : files) {
            String name = file.getName();
            //重新命名项目
            file = FileUtil.rename(file, StrUtil.replace(name, "-demo", "-" + code), true);

            rebuildPom(file, code);

            rebuildResource(file, desc, code,version);

            File demoPackage = FileUtil.file(file, "src/main/java/com/sl/plugin/step/demo");
            demoPackage = FileUtil.rename(demoPackage, code, true);

            rebuildJava(demoPackage, code);
        }
        FileUtil.del(compRootFile);

        File zip = ZipUtil.zip(unzip);

        FileUtil.del(unzip);

        String s = Base64.getEncoder().encodeToString(FileUtil.readBytes(zip));
        FileUtil.del(zip);
        return s;
    }

    private void rebuildJava(File demoPackage, String code) {
        commonRebuildJava(demoPackage,code,"DemoComp.java","Comp.java");
        commonRebuildJava(demoPackage,code,"DemoCompContext.java","CompContext.java");
        commonRebuildJava(demoPackage,code,"DemoCompDescriptor.java","CompDescriptor.java");
    }

    private void commonRebuildJava(File demoPackage, String code,String fileName,String targetFileName) {
        File compJavaFile = FileUtil.file(demoPackage, fileName);
        String name = StrUtil.upperFirst(code);
        compJavaFile = FileUtil.rename(compJavaFile, name + targetFileName, true);

        String compJavaFileText = FileUtil.readString(compJavaFile, Charset.defaultCharset());

        compJavaFileText=  StrUtil.replace(compJavaFileText,"demo",code);
        compJavaFileText = StrUtil.replace(compJavaFileText,"Demo",name);

        FileUtil.writeString(compJavaFileText,compJavaFile,Charset.defaultCharset());

    }

    private void rebuildResource(File file, String desc, String code, String version) {
        File pluginPropertiesFile = FileUtil.file(file, "src/main/resources/plugin.properties");

        String name = StrUtil.upperFirst(code);

        Map<String, Object> format = Maps.newHashMap();
        format.put("code", code);
        format.put("version", version);
        format.put("name", name);
        format.put("desc", desc);
        String html = StrUtil.format("version={version}\n" +
                "pluginCode={code}\n" +
                "pluginName={code}\n" +
                "desc={desc}\n" +
                "groupName=转换\n" +
                "metaClass=com.sl.plugin.step.{code}.{name}CompDescriptor", format);

        FileUtil.writeString(html, pluginPropertiesFile, Charset.defaultCharset());
    }

    private void rebuildPom(File file, String code) {

        File pomFile = FileUtil.file(file, "pom.xml");

        String pomText = FileUtil.readString(pomFile, Charset.defaultCharset());

        pomText = StrUtil.replace(pomText, "-demo", "-" + code);

        FileUtil.writeString(pomText, pomFile, Charset.defaultCharset());

    }

    public static File getBaseRootFile() {
        String property = System.getProperty("user.home");

        File file = FileUtil.file(property + File.separator + "square_lake" + File.separator + "compInitFile");
        if (!file.exists()) {
            synchronized (ProcessCompObjectInitFileHelper.class) {
                if (!file.exists()) {
                    file.mkdir();
                }
            }
        }
        return file;
    }

}
